package com.nhjf.mobile.controller.coursesSeries;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nhjf.common.page.Pagination;
import com.nhjf.mobile.common.util.SysProperties;
import com.nhjf.model.coursesSeries.CoursesSeries;
import com.nhjf.model.payLog.PayLog;
import com.nhjf.model.student.Student;
import com.nhjf.model.system.AdminSession;
import com.nhjf.model.system.User;
import com.nhjf.model.teachers.Courses;
import com.nhjf.model.teachers.CoursesCollection;
import com.nhjf.model.teachers.OrganizationHasStudent;
import com.nhjf.model.teachers.RecentlyStudied;
import com.nhjf.model.teachers.Teacher;
import com.nhjf.pojo.qo.coursesSeries.CoursesSeriesQo;
import com.nhjf.pojo.qo.payLog.PayLogQo;
import com.nhjf.pojo.qo.studnet.StudentQo;
import com.nhjf.pojo.qo.system.AdminSessionQo;
import com.nhjf.pojo.qo.system.UserQo;
import com.nhjf.pojo.qo.teachers.CoursesCollectionQo;
import com.nhjf.pojo.qo.teachers.CoursesCommentQo;
import com.nhjf.pojo.qo.teachers.CoursesQo;
import com.nhjf.pojo.qo.teachers.OrganizationHasStudentQo;
import com.nhjf.pojo.qo.teachers.RecentlyStudiedQo;
import com.nhjf.pojo.qo.teachers.TeachersQo;
import com.nhjf.service.coursesSeries.CoursesSeriesService;
import com.nhjf.service.payLog.PayLogService;
import com.nhjf.service.student.StudentService;
import com.nhjf.service.system.AdminSessionService;
import com.nhjf.service.system.UserService;
import com.nhjf.service.teachers.CoursesCollectionService;
import com.nhjf.service.teachers.CoursesCommentService;
import com.nhjf.service.teachers.CoursesService;
import com.nhjf.service.teachers.OrganizationHasStudentService;
import com.nhjf.service.teachers.RecentlyStudiedService;
import com.nhjf.service.teachers.TeachersService;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/coursesSeries")
public class CoursesSeriesController {

	@Autowired
	private CoursesSeriesService coursesSeriesService;

	@Autowired
	private CoursesService coursesService;
	@Autowired
	private UserService userService;
	@Autowired
	private TeachersService teacherService;
	@Autowired
	private CoursesCommentService coursesCommentService;
	@Autowired
	private CoursesCollectionService coursesCollectionService;
	@Autowired
	private AdminSessionService adminSessionService;
	@Autowired
	private RecentlyStudiedService  recentlyStudiedService;
	@Autowired
	private PayLogService payLogService;
	@Autowired
	private OrganizationHasStudentService hasStudentService;
	@Autowired
	private StudentService studentService;
	//课程列表
	@ResponseBody
	@RequestMapping(value = "/coursesSeriesPage")
	public String coursescpage() {
		Map<String, Object> resMap=new HashMap<String, Object>();
		Pagination pagination = new Pagination();
		pagination.setPageSize(8);
		CoursesSeriesQo coursesSeriesQo=new CoursesSeriesQo();
		coursesSeriesQo.setSeachTeacher(true);
		coursesSeriesQo.setIsReview(1);
		coursesSeriesQo.setCourseShares(1);
		pagination.setCondition(coursesSeriesQo);
		pagination = coursesSeriesService.queryPagination(pagination);
		List<CoursesSeries> coursesList=(List<CoursesSeries>)pagination.getList();
		SysProperties sysProperties=SysProperties.getInstance();
		String imagePath=sysProperties.get("imagePath");
		for(CoursesSeries courses:coursesList) {
			TeachersQo teachersQo=new TeachersQo();
			teachersQo.setTeacherId(courses.getUploader().getId());
			teachersQo.setSeachUser(true);
			Teacher teacher=teacherService.queryUnique(teachersQo);
			courses.setUserId(teacher.getUser().getId());
			if(StringUtils.isNotEmpty(courses.getUploader().getAvatar())) {
				if(courses.getUploader().getAvatar().indexOf(imagePath+"/picture/")>=0) {
				}else {
					courses.getUploader().setAvatar(imagePath+"/picture/"+courses.getUploader().getAvatar());
				}
			}
			courses.setCover(imagePath+"/img/"+courses.getCover());
			courses.setTotalDetails("");
			//			courses.setCourseintroduction("");
			//			courses.setCourseintroduction("");
			//			courses.setCourseTypeOneId(null);
			//			courses.setCourseTypeTwoId(null);
			//			courses.set
		}

		resMap.put("code", "200");
		resMap.put("message", coursesList);
		return JSONObject.fromObject(resMap).toString();
	}

	/**
	 * 老师课程列表
	 * @param teacherId
	 * @param page
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/coursesSeriesTeacherpage")
	public String coursesSeriesTeacherpage(@RequestParam(value="teacherId", required=true)String teacherId,@RequestParam(value = "page", defaultValue = "1")Integer page) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		Pagination pagination = new Pagination();
		pagination.setPageSize(8);
		CoursesSeriesQo qo=new CoursesSeriesQo();
		qo.setUploader(Long.parseLong(teacherId));
		qo.setSeachTeacher(true);
		qo.setIsReview(1);
		pagination.setCondition(qo);

		pagination.setPageNo(page);
		pagination = coursesSeriesService.queryPagination(pagination);
		List<CoursesSeries> coursesList=(List<CoursesSeries>)pagination.getList();
		SysProperties sysProperties=SysProperties.getInstance();
		String imagePath=sysProperties.get("imagePath");
		for(CoursesSeries courses:coursesList) {
			if(StringUtils.isNotEmpty(courses.getUploader().getAvatar())) {
				if(courses.getUploader().getAvatar().indexOf(imagePath+"/picture/")>=0) {
				}else {
					courses.getUploader().setAvatar(imagePath+"/picture/"+courses.getUploader().getAvatar());
				}
			}
			courses.setCover(imagePath+"/img/"+courses.getCover());
			courses.setTotalDetails("");
			//			courses.setCourseintroduction("");
			//			courses.setCourseintroduction("");
			//			courses.setCourseTypeOneId(null);
			//			courses.setCourseTypeTwoId(null);
			//			courses.set
		}

		resMap.put("code", "200");
		resMap.put("message", pagination);
		return JSONObject.fromObject(resMap).toString();
	}
	/**
	 * 课程详情
	 * @param coursesId
	 * @param token
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/coursesSeriesDetails")
	public String coursesSeriesDetails( @RequestParam(value="coursesId", required=true) String coursesId,@RequestParam(value="token", required=false) String token) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		CoursesSeriesQo qo=new CoursesSeriesQo();
		qo.setCoursesSeriesId(Long.parseLong(coursesId));
		qo.setSeachTeacher(true);
		CoursesSeries courses=coursesSeriesService.queryUnique(qo);
		if(courses!=null) {
			TeachersQo teacherQo=new TeachersQo();
			teacherQo.setTeacherId(courses.getUploader().getId());
			teacherQo.setSeachUser(true);
			Teacher teacher=teacherService.queryUnique(teacherQo);
			courses.setSelfintroduction(teacher.getUser().getSelfintroduction());
			courses.setPopularity((courses.getPopularity()==null?0:courses.getPopularity())+1);
			coursesSeriesService.update(courses);
			CoursesCommentQo coursesCommentQo=new CoursesCommentQo();
			coursesCommentQo.setCoursesId(Long.parseLong(coursesId));
			Integer coursesCommentCount=coursesCommentService.queryCount(coursesCommentQo);
			CoursesCollectionQo coursesCollectionQo=new CoursesCollectionQo();
			coursesCollectionQo.setCoursesId(Long.parseLong(coursesId));
			Integer coursesCollectionCount=coursesCollectionService.queryCount(coursesCollectionQo);
			courses.setCoursesCommentCount(coursesCommentCount);
			courses.setCoursesCollectionCount(coursesCollectionCount);
			courses.setUserId(teacher.getUser().getId());

			CoursesQo coursesQo=new CoursesQo();
			coursesQo.setSeriesId(courses.getId());
			coursesQo.setTimeSort(false);
			List<Courses> coursesList=coursesService.queryList(coursesQo);
			courses.setCoursesList(coursesList);
			if(StringUtils.isNotEmpty(token)) {
				AdminSessionQo adminSessionQo=new AdminSessionQo();
				adminSessionQo.setToken(token);
				AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
				if(adminSession!=null) {
					UserQo userQo=new UserQo();
					userQo.setUserName(adminSession.getLoginName());
					User user= userService.queryUnique(userQo);
					if(user!=null) {
						CoursesCollectionQo coursesCollectionQo1=new CoursesCollectionQo();
						coursesCollectionQo1.setUserId(user.getId());
						coursesCollectionQo1.setCoursesId(Long.parseLong(coursesId));
						CoursesCollection collection1=coursesCollectionService.queryUnique(coursesCollectionQo1);
						if(collection1!=null) {
							courses.setIsCollection(1);
						}
						RecentlyStudiedQo recentlyStudiedQo=new RecentlyStudiedQo();
						recentlyStudiedQo.setSeachCourses(true);
						recentlyStudiedQo.setSeachTeacher(true);
						recentlyStudiedQo.setSeachUser(true);
						recentlyStudiedQo.setCoursesId(Long.parseLong(coursesId));
						recentlyStudiedQo.setUserId(user.getId());
						RecentlyStudied recentlyStudied1= recentlyStudiedService.queryUnique(recentlyStudiedQo);
						if(recentlyStudied1!=null) {
							recentlyStudied1.setRecentLearnTime(new Date());
							recentlyStudiedService.update(recentlyStudied1);
						}else {
							//最近观看
							RecentlyStudied recentlyStudied=new RecentlyStudied();
							recentlyStudied.setTeacher(courses.getUploader());
							recentlyStudied.setUser(user);
							recentlyStudied.setRecentLearnTime(new Date());
							recentlyStudied.setCourses(courses);
							recentlyStudiedService.save(recentlyStudied);
						}

					}
				}
			}
			SysProperties sysProperties=SysProperties.getInstance();
			String imagePath=sysProperties.get("imagePath");
			if(courses.getIsPrice()!=null&&courses.getIsPrice()==1) {

				if(courses.getCourseShare()==2) {
					if(courses.getUploader()!=null&&courses.getUploader().getAvatar()!=null){
						courses.getUploader().setAvatar(imagePath+"/picture/"+courses.getUploader().getAvatar());
					}
					if(courses.getUploader()!=null) {
						TeachersQo qo2=new TeachersQo();
						qo2.setSeachUser(true);
						qo2.setTeacherId(courses.getUploader().getId());
						Teacher teacher2=teacherService.queryUnique(qo2);
						courses.getUploader().setFans(teacher2.getUser().getFans());
					}
					courses.setCoursesStatus(2);
					resMap.put("code", "200");
					for(Courses cours:coursesList){
						cours.setVideo(imagePath+"/videofile/"+cours.getVideo());
					}
					courses.setCoursesList(coursesList);
					//					courses.setVideo(imagePath+"/videofile/"+courses.getVideo());
					courses.setCover(imagePath+"/img/"+courses.getCover());
					resMap.put("message", courses);
					return JSONObject.fromObject(resMap).toString();
				}else if(courses.getCourseShare()==1) {
					if(StringUtils.isNotEmpty(token)) {
						AdminSessionQo adminSessionQo=new AdminSessionQo();
						adminSessionQo.setToken(token);
						AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
						if(adminSession!=null) {
							UserQo userQo=new UserQo();
							userQo.setUserName(adminSession.getLoginName());
							User user= userService.queryUnique(userQo);
							if(user!=null) {
								if(user.getIsRole()==2) {
									TeachersQo teachersQo=new TeachersQo();
									teachersQo.setSeachOrganization(true);
									teachersQo.setTeacherId(courses.getUploader().getId());
									Teacher teacher2=teacherService.queryUnique(teachersQo); 
									OrganizationHasStudentQo hasStudentQo=new OrganizationHasStudentQo();
									StudentQo studentQo=new StudentQo();
									studentQo.setUserid(user.getId());
									Student student= studentService.queryUnique(studentQo);
									hasStudentQo.setStudentId(student.getId());
									hasStudentQo.setOrganizationId(teacher2.getTraininginstitution().getId());
									OrganizationHasStudent hasStudent= hasStudentService.queryUnique(hasStudentQo);
									if(hasStudent!=null) {
										if(courses.getUploader()!=null&&courses.getUploader().getAvatar()!=null){
											courses.getUploader().setAvatar(imagePath+"/picture/"+courses.getUploader().getAvatar());
										}
										if(courses.getUploader()!=null) {
											TeachersQo qo2=new TeachersQo();
											qo2.setSeachUser(true);
											qo2.setTeacherId(courses.getUploader().getId());
											Teacher teacher3=teacherService.queryUnique(qo2);
											courses.getUploader().setFans(teacher3.getUser().getFans());
										}
										resMap.put("code", "200");
										for(Courses cours:coursesList){
											cours.setVideo(imagePath+"/videofile/"+cours.getVideo());
										}
										courses.setCoursesList(coursesList);
										//									courses.setVideo(imagePath+"/videofile/"+courses.getVideo());
										courses.setCover(imagePath+"/img/"+courses.getCover());
										courses.setCoursesStatus(2);
										resMap.put("message", courses);
										return JSONObject.fromObject(resMap).toString();
									}else {

										courses.setCoursesStatus(3);
										for(Courses cours:coursesList){
											if(cours.getIsPay()==1) {
												cours.setVideo(imagePath+"/videofile/"+cours.getVideo());
											}else {
												cours.setVideo("");
											}
										}
										courses.setCover(imagePath+"/img/"+courses.getCover());
										resMap.put("code", "200");
										resMap.put("message", courses);
										return JSONObject.fromObject(resMap).toString();
									}

								}else {
									TeachersQo teachersQo=new TeachersQo();
									teachersQo.setSeachOrganization(true);
									teachersQo.setUserId(user.getId());
									Teacher teacher2= teacherService.queryUnique(teachersQo);
									TeachersQo teachersQo2=new TeachersQo();
									teachersQo2.setSeachOrganization(true);
									teachersQo2.setTeacherId(courses.getUploader().getId());
									Teacher teacher3= teacherService.queryUnique(teachersQo2);
									if(teacher2.getTraininginstitution().getId()==teacher3.getTraininginstitution().getId()) {
										if(courses.getUploader()!=null&&courses.getUploader().getAvatar()!=null){
											courses.getUploader().setAvatar(imagePath+"/picture/"+courses.getUploader().getAvatar());
										}
										if(courses.getUploader()!=null) {
											TeachersQo qo2=new TeachersQo();
											qo2.setSeachUser(true);
											qo2.setTeacherId(courses.getUploader().getId());
											Teacher teacher4=teacherService.queryUnique(qo2);
											courses.getUploader().setFans(teacher4.getUser().getFans());
										}
										resMap.put("code", "200");
										for(Courses cours:coursesList){
											cours.setVideo(imagePath+"/videofile/"+cours.getVideo());
										}
										courses.setCoursesList(coursesList);
										courses.setCoursesStatus(2);
										//									courses.setVideo(imagePath+"/videofile/"+courses.getVideo());
										courses.setCover(imagePath+"/img/"+courses.getCover());
										resMap.put("message", courses);
										return JSONObject.fromObject(resMap).toString();
									}else {
										courses.setCoursesStatus(3);
										for(Courses cours:coursesList){
											if(cours.getIsPay()==1) {
												cours.setVideo(imagePath+"/videofile/"+cours.getVideo());
											}else {
												cours.setVideo("");
											}
										}
										courses.setCover(imagePath+"/img/"+courses.getCover());
										resMap.put("code", "200");
										resMap.put("message", courses);
										return JSONObject.fromObject(resMap).toString();
									}
								}
							}else {
								if(courses.getCourseShare()==1) {
									courses.setCoursesStatus(3);
									for(Courses cours:coursesList){
										if(cours.getIsPay()==1) {
											cours.setVideo(imagePath+"/videofile/"+cours.getVideo());
										}else {
											cours.setVideo("");
										}
									}
									courses.setCover(imagePath+"/img/"+courses.getCover());
									resMap.put("code", "200");
									resMap.put("message", courses);
									return JSONObject.fromObject(resMap).toString();
								}else if(courses.getCourseShare()==2){
									if(courses.getUploader()!=null&&courses.getUploader().getAvatar()!=null){
										courses.getUploader().setAvatar(imagePath+"/picture/"+courses.getUploader().getAvatar());
									}
									if(courses.getUploader()!=null) {
										TeachersQo qo2=new TeachersQo();
										qo2.setSeachUser(true);
										qo2.setTeacherId(courses.getUploader().getId());
										Teacher teacher2=teacherService.queryUnique(qo2);
										courses.getUploader().setFans(teacher2.getUser().getFans());
									}
									resMap.put("code", "200");
									for(Courses cours:coursesList){
										cours.setVideo(imagePath+"/videofile/"+cours.getVideo());
									}
									courses.setCoursesList(coursesList);
									courses.setCoursesStatus(2);
									//									courses.setVideo(imagePath+"/videofile/"+courses.getVideo());
									courses.setCover(imagePath+"/img/"+courses.getCover());
									resMap.put("message", courses);
									return JSONObject.fromObject(resMap).toString();
								}else {
									courses.setCoursesStatus(3);
									for(Courses cours:coursesList){
										if(cours.getIsPay()==1) {
											cours.setVideo(imagePath+"/videofile/"+cours.getVideo());
										}else {
											cours.setVideo("");
										}
									}
									courses.setCover(imagePath+"/img/"+courses.getCover());
									resMap.put("code", "200");
									resMap.put("message", courses);
									return JSONObject.fromObject(resMap).toString();
								}
							}
						}

					}
				}else {
					for(Courses cours:coursesList){
						if(cours.getIsPay()==1) {
							cours.setVideo(imagePath+"/videofile/"+cours.getVideo());
						}else {
							cours.setVideo("");
						}
					}
					courses.setCoursesList(coursesList);
					courses.setCoursesStatus(3);
					courses.setCover(imagePath+"/img/"+courses.getCover());
					resMap.put("code", "200");
					resMap.put("message", courses);
					return JSONObject.fromObject(resMap).toString();
				}

			}else {


				if(courses.getUploader()!=null&&courses.getUploader().getAvatar()!=null){
					courses.getUploader().setAvatar(imagePath+"/picture/"+courses.getUploader().getAvatar());
				}
				if(courses.getUploader()!=null) {
					TeachersQo qo2=new TeachersQo();
					qo2.setSeachUser(true);
					qo2.setTeacherId(courses.getUploader().getId());
					Teacher teacher2=teacherService.queryUnique(qo2);
					courses.getUploader().setFans(teacher2.getUser().getFans());
				}

				if(StringUtils.isNotEmpty(token)) {
					AdminSessionQo adminSessionQo=new AdminSessionQo();
					adminSessionQo.setToken(token);
					AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
					if(adminSession!=null) {
						UserQo userQo=new UserQo();
						userQo.setUserName(adminSession.getLoginName());
						User user= userService.queryUnique(userQo);
						if(user!=null) {
							if(courses.getCourseShare()==3) {
								if(user.getIsRole()==1 || user.getIsRole()==3) {
									TeachersQo teachersQo=new TeachersQo();
									teachersQo.setSeachOrganization(true);
									teachersQo.setUserId(user.getId());
									Teacher teacher2= teacherService.queryUnique(teachersQo);
									TeachersQo teachersQo2=new TeachersQo();
									teachersQo2.setSeachOrganization(true);
									teachersQo2.setTeacherId(courses.getUploader().getId());
									Teacher teacher3= teacherService.queryUnique(teachersQo2);
									if(teacher2.getTraininginstitution().getId()==teacher3.getTraininginstitution().getId()) {
										courses.setSuccessPay(2);
										courses.setCoursesStatus(2);
										for(Courses cours:coursesList){
											cours.setVideo(imagePath+"/videofile/"+cours.getVideo());
										}
										courses.setCover(imagePath+"/img/"+courses.getCover());
										resMap.put("code", "200");
										resMap.put("message", courses);
										return JSONObject.fromObject(resMap).toString();
									}else {
										PayLogQo payLogQo=new PayLogQo();
										payLogQo.setUser(user.getId());
										payLogQo.setCoursesSeries(courses.getId());
										payLogQo.setStatus(2);
										PayLog payLog=payLogService.queryUnique(payLogQo);
										if(payLog!=null){
											courses.setCoursesStatus(2);
											courses.setSuccessPay(2);
											for(Courses cours:coursesList){
												cours.setVideo(imagePath+"/videofile/"+cours.getVideo());
											}
											courses.setCover(imagePath+"/img/"+courses.getCover());
											resMap.put("code", "200");
											resMap.put("message", courses);
											return JSONObject.fromObject(resMap).toString();
										}else{
											courses.setCoursesStatus(1);
											courses.setSuccessPay(1);
											for(Courses cours:coursesList){
												if(cours.getIsPay()==1) {
													cours.setVideo(imagePath+"/videofile/"+cours.getVideo());
												}else {
													cours.setVideo("");
												}
											}
											courses.setCover(imagePath+"/img/"+courses.getCover());
											resMap.put("code", "200");
											resMap.put("message", courses);
											return JSONObject.fromObject(resMap).toString();
										}
									}
								}else {
									TeachersQo teachersQo=new TeachersQo();
									teachersQo.setSeachOrganization(true);
									teachersQo.setTeacherId(courses.getUploader().getId());
									Teacher teacher2=teacherService.queryUnique(teachersQo); 
									OrganizationHasStudentQo hasStudentQo=new OrganizationHasStudentQo();
									hasStudentQo.setOrganizationId(teacher2.getTraininginstitution().getId());
									StudentQo studentQo=new StudentQo();
									studentQo.setUserid(user.getId());
									Student student= studentService.queryUnique(studentQo);
									hasStudentQo.setStudentId(student.getId());
									OrganizationHasStudent hasStudent= hasStudentService.queryUnique(hasStudentQo);
									if(hasStudent!=null) {
										courses.setSuccessPay(2);
										courses.setCoursesStatus(2);
										for(Courses cours:coursesList){
											cours.setVideo(imagePath+"/videofile/"+cours.getVideo());
										}
										courses.setCover(imagePath+"/img/"+courses.getCover());
										resMap.put("code", "200");
										resMap.put("message", courses);
										return JSONObject.fromObject(resMap).toString();
									}else {
										PayLogQo payLogQo=new PayLogQo();
										payLogQo.setUser(user.getId());
										payLogQo.setCoursesSeries(courses.getId());
										payLogQo.setStatus(2);
										PayLog payLog=payLogService.queryUnique(payLogQo);
										if(payLog!=null){
											courses.setCoursesStatus(2);
											courses.setSuccessPay(2);
											for(Courses cours:coursesList){
												cours.setVideo(imagePath+"/videofile/"+cours.getVideo());
											}
											courses.setCover(imagePath+"/img/"+courses.getCover());
											resMap.put("code", "200");
											resMap.put("message", courses);
											return JSONObject.fromObject(resMap).toString();
										}else{
											courses.setCoursesStatus(1);
											courses.setSuccessPay(1);
											for(Courses cours:coursesList){
												if(cours.getIsPay()==1) {
													cours.setVideo(imagePath+"/videofile/"+cours.getVideo());
												}else {
													cours.setVideo("");
												}
											}
											courses.setCover(imagePath+"/img/"+courses.getCover());
											resMap.put("code", "200");
											resMap.put("message", courses);
											return JSONObject.fromObject(resMap).toString();
										}
									}
								}

							}
							else if(courses.getCourseShare()==2) {
								PayLogQo payLogQo=new PayLogQo();
								payLogQo.setUser(user.getId());
								payLogQo.setCoursesSeries(courses.getId());
								payLogQo.setStatus(2);
								PayLog payLog=payLogService.queryUnique(payLogQo);
								if(payLog!=null){
									courses.setCoursesStatus(2);
									courses.setSuccessPay(2);
									for(Courses cours:coursesList){
										cours.setVideo(imagePath+"/videofile/"+cours.getVideo());
									}
									courses.setCover(imagePath+"/img/"+courses.getCover());
									resMap.put("code", "200");
									resMap.put("message", courses);
									return JSONObject.fromObject(resMap).toString();
								}else{
									courses.setCoursesStatus(1);
									courses.setSuccessPay(1);
									for(Courses cours:coursesList){
										if(cours.getIsPay()==1) {
											cours.setVideo(imagePath+"/videofile/"+cours.getVideo());
										}else {
											cours.setVideo("");
										}
									}
									courses.setCover(imagePath+"/img/"+courses.getCover());
									resMap.put("code", "200");
									resMap.put("message", courses);
									return JSONObject.fromObject(resMap).toString();
								}
							}else if(courses.getCourseShare()==1) {
								if(user.getIsRole()==2) {
									TeachersQo teachersQo=new TeachersQo();
									teachersQo.setSeachOrganization(true);
									teachersQo.setTeacherId(courses.getUploader().getId());
									Teacher teacher2=teacherService.queryUnique(teachersQo); 
									OrganizationHasStudentQo hasStudentQo=new OrganizationHasStudentQo();
									StudentQo studentQo=new StudentQo();
									studentQo.setUserid(user.getId());
									Student student= studentService.queryUnique(studentQo);
									hasStudentQo.setStudentId(student.getId());
									hasStudentQo.setOrganizationId(teacher2.getTraininginstitution().getId());
									OrganizationHasStudent hasStudent= hasStudentService.queryUnique(hasStudentQo);
									if(hasStudent!=null) {
										PayLogQo payLogQo=new PayLogQo();
										payLogQo.setUser(user.getId());
										payLogQo.setCoursesSeries(courses.getId());
										payLogQo.setStatus(2);
										PayLog payLog=payLogService.queryUnique(payLogQo);
										if(payLog!=null){
											courses.setCoursesStatus(2);
											courses.setCoursesStatus(1);
											courses.setSuccessPay(2);
											for(Courses cours:coursesList){
												cours.setVideo(imagePath+"/videofile/"+cours.getVideo());
											}
											courses.setCover(imagePath+"/img/"+courses.getCover());
											resMap.put("code", "200");
											resMap.put("message", courses);
											return JSONObject.fromObject(resMap).toString();
										}else{
											courses.setCoursesStatus(1);
											courses.setSuccessPay(1);
											for(Courses cours:coursesList){
												if(cours.getIsPay()==1) {
													cours.setVideo(imagePath+"/videofile/"+cours.getVideo());
												}else {
													cours.setVideo("");
												}
											}
											courses.setCover(imagePath+"/img/"+courses.getCover());
											resMap.put("code", "200");
											resMap.put("message", courses);
											return JSONObject.fromObject(resMap).toString();
										}
									}else {
										courses.setCoursesStatus(3);
										for(Courses cours:coursesList){
											if(cours.getIsPay()==1) {
												cours.setVideo(imagePath+"/videofile/"+cours.getVideo());
											}else {
												cours.setVideo("");
											}
										}
										courses.setCover(imagePath+"/img/"+courses.getCover());
										resMap.put("code", "200");
										resMap.put("message", courses);
										return JSONObject.fromObject(resMap).toString();
									}
								}else {
									TeachersQo teachersQo=new TeachersQo();
									teachersQo.setSeachOrganization(true);
									teachersQo.setUserId(user.getId());
									Teacher teacher2= teacherService.queryUnique(teachersQo);
									TeachersQo teachersQo2=new TeachersQo();
									teachersQo2.setSeachOrganization(true);
									teachersQo2.setTeacherId(courses.getUploader().getId());
									Teacher teacher3= teacherService.queryUnique(teachersQo2);
									if(teacher2.getTraininginstitution().getId()==teacher3.getTraininginstitution().getId()) {
										PayLogQo payLogQo=new PayLogQo();
										payLogQo.setUser(user.getId());
										payLogQo.setCoursesSeries(courses.getId());
										payLogQo.setStatus(2);
										PayLog payLog=payLogService.queryUnique(payLogQo);
										if(payLog!=null){
											courses.setCoursesStatus(2);
											courses.setSuccessPay(2);
											for(Courses cours:coursesList){
												cours.setVideo(imagePath+"/videofile/"+cours.getVideo());
											}
											courses.setCover(imagePath+"/img/"+courses.getCover());
											resMap.put("code", "200");
											resMap.put("message", courses);
											return JSONObject.fromObject(resMap).toString();
										}else{
											courses.setCoursesStatus(1);
											courses.setSuccessPay(1);
											for(Courses cours:coursesList){
												if(cours.getIsPay()==1) {
													cours.setVideo(imagePath+"/videofile/"+cours.getVideo());
												}else {
													cours.setVideo("");
												}
											}
											courses.setCover(imagePath+"/img/"+courses.getCover());
											resMap.put("code", "200");
											resMap.put("message", courses);
											return JSONObject.fromObject(resMap).toString();
										}
									}else {
										for(Courses cours:coursesList){
											if(cours.getIsPay()==1) {
												cours.setVideo(imagePath+"/videofile/"+cours.getVideo());
											}else {
												cours.setVideo("");
											}
										}
										courses.setCover(imagePath+"/img/"+courses.getCover());
										courses.setCoursesStatus(3);
										resMap.put("code", "200");
										resMap.put("message", courses);
										return JSONObject.fromObject(resMap).toString();
									}
								}

							}else {
								for(Courses cours:coursesList){
									if(cours.getIsPay()==1) {
										cours.setVideo(imagePath+"/videofile/"+cours.getVideo());
									}else {
										cours.setVideo("");
									}
								}
								resMap.put("code", "500");
								resMap.put("message","课程信息错误!");
								return JSONObject.fromObject(resMap).toString();
							}
						}else{
							courses.setCoursesStatus(1);
							for(Courses cours:coursesList){
								if(cours.getIsPay()==1) {
									cours.setVideo(imagePath+"/videofile/"+cours.getVideo());
								}else {
									cours.setVideo("");
								}
							}
						}
					}else{
						courses.setCoursesStatus(1);
						for(Courses cours:coursesList){
							if(cours.getIsPay()==1) {
								cours.setVideo(imagePath+"/videofile/"+cours.getVideo());
							}else {
								cours.setVideo("");
							}
						}
					}


				}else{
					courses.setCoursesStatus(1);
					for(Courses cours:coursesList){
						if(cours.getIsPay()==1) {
							cours.setVideo(imagePath+"/videofile/"+cours.getVideo());
						}else {
							if(cours.getIsPay()==1) {
								cours.setVideo(imagePath+"/videofile/"+cours.getVideo());
							}else {
								cours.setVideo("");
							}
						}
						
					}
				}

				courses.setCoursesList(coursesList);
				courses.setCover(imagePath+"/img/"+courses.getCover());
				resMap.put("code", "200");
				resMap.put("message", courses);
				return JSONObject.fromObject(resMap).toString();

			}
		}else {

			resMap.put("code", "500");
			resMap.put("message","课程信息错误!");
			return JSONObject.fromObject(resMap).toString();
		}
		resMap.put("code", "500");
		resMap.put("message", "课程信息错误!");
		return JSONObject.fromObject(resMap).toString();
	}

	/**
	 * 二级课程列表
	 * @param typeOne
	 * @param typeTwo
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/coursesSeriesTypepage")
	public String coursesSeriesTypepage( @RequestParam(value="typeOne", required=true) String typeOne,String typeTwo) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		CoursesSeriesQo qo=new CoursesSeriesQo();
		qo.setCourseTypeOne(Long.parseLong(typeOne));
		qo.setCourseShares(1);
		qo.setCourseTypeTwo(Long.parseLong(typeTwo));
		qo.setSeachTeacher(true);
		qo.setIsReview(1);
		Pagination pagination = new Pagination();
		pagination.setPageSize(8);
		pagination.setCondition(qo);
		pagination = coursesSeriesService.queryPagination(pagination);
		List<CoursesSeries> coursesList=(List<CoursesSeries>)pagination.getList();
		SysProperties sysProperties=SysProperties.getInstance();
		String imagePath=sysProperties.get("imagePath");
		for(CoursesSeries courses:coursesList) {
			TeachersQo teachersQo=new TeachersQo();
			teachersQo.setTeacherId(courses.getUploader().getId());
			teachersQo.setSeachUser(true);
			Teacher teacher=teacherService.queryUnique(teachersQo);
			if(StringUtils.isNotEmpty(courses.getUploader().getAvatar())) {

				if(courses.getUploader().getAvatar().indexOf(imagePath+"/picture/")>=0) {
				}else {
					courses.getUploader().setAvatar(imagePath+"/picture/"+courses.getUploader().getAvatar());
				}
			}
			courses.setUserId(teacher.getUser().getId());
			courses.setCover(imagePath+"/img/"+courses.getCover());
			courses.setTotalDetails("");
			//			courses.setCourseintroduction("");
			//			courses.setCourseintroduction("");
			//			courses.setCourseTypeOneId(null);
			//			courses.setCourseTypeTwoId(null);
			//			courses.set
		}

		resMap.put("code", "200");
		resMap.put("message", coursesList);
		return JSONObject.fromObject(resMap).toString();
	}
	/**
	 * 二级课程列表分页
	 * @param typeOne
	 * @param typeTwo
	 * @param page
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/coursesSeriesType")
	public String coursesSeriesType( @RequestParam(value="typeOne", required=true) String typeOne,String typeTwo,@RequestParam(value = "page", defaultValue = "1")Integer page) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		CoursesSeriesQo qo=new CoursesSeriesQo();
		qo.setCourseTypeOne(Long.parseLong(typeOne));
		qo.setSeachTeacher(true);
		qo.setIsReview(1);
		qo.setCourseShares(1);
		if(StringUtils.isNotEmpty(typeTwo)&&!"null".equals(typeTwo)) {
			qo.setCourseTypeTwo(Long.parseLong(typeTwo));
		}
		Pagination pagination = new Pagination();
		pagination.setPageSize(10);
		pagination.setCondition(qo);
		pagination.setPageNo(page);
		pagination = coursesSeriesService.queryPagination(pagination);
		List<CoursesSeries> coursesList=(List<CoursesSeries>)pagination.getList();
		SysProperties sysProperties=SysProperties.getInstance();
		String imagePath=sysProperties.get("imagePath");
		for(CoursesSeries courses:coursesList) {
			TeachersQo teachersQo=new TeachersQo();
			teachersQo.setTeacherId(courses.getUploader().getId());
			teachersQo.setSeachUser(true);
			Teacher teacher=teacherService.queryUnique(teachersQo);
			if(StringUtils.isNotEmpty(courses.getUploader().getAvatar())) {
				if(courses.getUploader().getAvatar().indexOf(imagePath+"/picture/")>=0) {

				}else {
					courses.getUploader().setAvatar(imagePath+"/picture/"+courses.getUploader().getAvatar());
				}
			}
			courses.setUserId(teacher.getUser().getId());
			courses.setCover(imagePath+"/img/"+courses.getCover());
			courses.setTotalDetails("");
			//			courses.setCourseintroduction("");
			//			courses.setCourseintroduction("");
			//			courses.setCourseTypeOneId(null);
			//			courses.setCourseTypeTwoId(null);
			//			courses.set
		}

		resMap.put("code", "200");
		resMap.put("message", pagination);
		return JSONObject.fromObject(resMap).toString();
	}
	/**
	 * 一级课程列表
	 * @param typeOne
	 * @param page
	 * @param typeTwo
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/coursesSeriesTypeOnepage")
	public String coursesSeriesTypeOnepage( @RequestParam(value="typeOne", required=false) String typeOne,@RequestParam(value = "page", defaultValue = "1")Integer page,@RequestParam(value="typeTwo", required=false)String typeTwo) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		CoursesSeriesQo qo=new CoursesSeriesQo();
		qo.setIsReview(1);
		qo.setCourseTypeOne(Long.parseLong(typeOne));
		qo.setCourseTypeTwo(Long.parseLong(typeTwo));
		Pagination pagination = new Pagination();
		pagination.setPageSize(10);
		pagination.setPageNo(page);
		pagination.setCondition(qo);
		pagination = coursesSeriesService.queryPagination(pagination);
		List<CoursesSeries> coursesList=(List<CoursesSeries>)pagination.getList();
		SysProperties sysProperties=SysProperties.getInstance();
		String imagePath=sysProperties.get("imagePath");
		for(CoursesSeries courses:coursesList) {
			courses.setCover(imagePath+"/img/"+courses.getCover());
			courses.setTotalDetails("");
			//			courses.setCourseintroduction("");
			//			courses.setCourseintroduction("");
			//			courses.setCourseTypeOneId(null);
			//			courses.setCourseTypeTwoId(null);
			//			courses.set
		}
		pagination.setList(coursesList);
		resMap.put("code", "200");
		resMap.put("message", pagination);
		return JSONObject.fromObject(resMap).toString();
	}
	/**
	 * 绝学列表
	 * @param typeOne
	 * @param page
	 * @param typeTwo
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/coursesSeriesSchooling")
	public String coursesSeriesSchooling( @RequestParam(value="typeOne", required=false) String typeOne,@RequestParam(value = "page", defaultValue = "1")Integer page,@RequestParam(value="typeTwo", required=false)String typeTwo) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		CoursesSeriesQo qo=new CoursesSeriesQo();
		qo.setIsReview(1);
		if(qo.getCourseTypeOne()!=null) {
			qo.setCourseTypeOne(Long.parseLong(typeOne));
		}
		if(qo.getCourseTypeTwo()!=null) {
			qo.setCourseTypeTwo(Long.parseLong(typeTwo));
		}
		qo.setSeachTeacher(true);
		qo.setIsSchooling(1);
		Pagination pagination = new Pagination();
		pagination.setPageSize(10);
		pagination.setPageNo(page);
		pagination.setCondition(qo);
		pagination = coursesSeriesService.queryPagination(pagination);
		List<CoursesSeries> coursesList=(List<CoursesSeries>)pagination.getList();
		SysProperties sysProperties=SysProperties.getInstance();
		String imagePath=sysProperties.get("imagePath");
		for(CoursesSeries courses:coursesList) {
			TeachersQo teachersQo=new TeachersQo();
			teachersQo.setTeacherId(courses.getUploader().getId());
			teachersQo.setSeachUser(true);
			Teacher teacher=teacherService.queryUnique(teachersQo);
			if(StringUtils.isNotEmpty(courses.getUploader().getAvatar())) {
				if(courses.getUploader().getAvatar().indexOf(imagePath+"/picture/")>=0) {

				}else {
					courses.getUploader().setAvatar(imagePath+"/picture/"+courses.getUploader().getAvatar());
				}
			}
			courses.setUserId(teacher.getUser().getId());
			courses.setCover(imagePath+"/img/"+courses.getCover());
			courses.setTotalDetails("");
			//			courses.setCourseintroduction("");
			//			courses.setCourseintroduction("");
			//			courses.setCourseTypeOneId(null);
			//			courses.setCourseTypeTwoId(null);
			//			courses.set
		}
		pagination.setList(coursesList);
		resMap.put("code", "200");
		resMap.put("message", pagination);
		return JSONObject.fromObject(resMap).toString();
	}


}
